package i.i.myapplication.controller;

//package i.i.myapplication.controller;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import i.i.myapplication.R;
import i.i.myapplication.model.Video;

//package i.i.myapplication.controller;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import i.i.myapplication.R;
import i.i.myapplication.model.Video;

public class VideoAdapter extends BaseAdapter {

    public interface OnPlayClickListener {
        void onPlayClick(int position);
    }

    private Context context;
    private List<Video> videoList;
    private OnPlayClickListener playClickListener;

    public VideoAdapter(Context context, List<Video> videoList) {
        this.context = context;
        this.videoList = videoList;
        this.playClickListener = null; // 没有点击回调
    }


    @Override
    public int getCount() {
        return videoList.size();
    }

    @Override
    public Object getItem(int position) {
        return videoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        ImageView imageView;
        TextView titleView;
        TextView descView;
        ImageView playButton;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.video_item_layout, parent, false);
            holder = new ViewHolder();
            holder.imageView = convertView.findViewById(R.id.video_img);
            holder.titleView = convertView.findViewById(R.id.video_title);
            holder.descView = convertView.findViewById(R.id.video_desc);
            holder.playButton = convertView.findViewById(R.id.video_play_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Video video = videoList.get(position);
        holder.imageView.setImageResource(video.getImageResId());
        holder.titleView.setText(video.getTitle());
        holder.descView.setText(video.getDescription());
//        holder.playButton.setOnClickListener(v -> {
//            if (playClickListener != null) {
//                playClickListener.onPlayClick(position);
//            }
//        });

        return convertView;
    }
}
